/*
 *所有关于DealWith类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*DealWithService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using XenFin.Read.System.IRepositories;
using SqlSugar;
using System;
using VOL.Core.DBManager;

namespace XenFin.Read.System.Services
{
    public partial class DealWithService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IDealWithRepository _repository;//访问数据库
        private readonly ISqlSugarClient _db;

        [ActivatorUtilitiesConstructor]
        public DealWithService(
            IDealWithRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            ISqlSugarClient db
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _db = db;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="saveDataModel"></param>
        /// <returns></returns>
        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            //此处saveModel是从前台提交的原生数据，可对数据进修改过滤
            AddOnExecute = (SaveModel saveModel) =>
            {
                //如果返回false,后面代码不会再执行
                return webResponse.OK();
            };
            // 在保存数据库前的操作，所有数据都验证通过了，这一步执行完就执行数据库保存
            AddOnExecuting = (DealWith dealWith, object list) =>
            {
                var prefix = DateTime.Now.ToString("yyyyMMdd");
                var lastCategory = _db.Queryable<DealWith>()
                                       .OrderByDescending(it => it.DealWithId)
                                       .FirstOrDefault();

                int number = 0;
                if (lastCategory != null)
                {
                    number = int.Parse(lastCategory.DealWithId.Substring(lastCategory.DealWithId.Length - 4)) + 1;
                }
                else
                {
                    number = 1;
                }

                dealWith.DealWithId = $"{"DEAL" + prefix}{number.ToString().PadLeft(4, '0')}";

                dealWith.ActualPrice = 0;
                dealWith.ResiduePrice = dealWith.DealWithPrice;
                dealWith.CreateTime = DateTime.Now;
                dealWith.PayReceState = 1;
                dealWith.IsDeleted = 0;

                return webResponse.OK();
            };

            return base.Add(saveDataModel);
        }
    }
}
